/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hibernate.dao;

import hibernate.entity.Accounts;
import hibernate.entity.ResetRequests;

/**
 *
 * @author Neo
 */
public class ResetPasswordDAO extends AbstractDAO {

    AccountDAO accDao = new AccountDAO();

    /**
     * @param genCode a automatically generated code
     * @param email an email
     * @return reset password
     * @see reset password page to enter email
     */
    public ResetRequests getByCodeAndEmail(String genCode, String email) {
        query = session.createQuery("from ResetRequests where genCode=:genCode and email = :email ");
        query.setParameter("genCode", genCode);
        query.setParameter("email", email);
        try {
            return (ResetRequests) query.list().get(0);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * @param email an email for reset password
     * @param newPass a new password to change
     * @return int
     */
    public int resetPassword(String email, String newPass) {
        try {
            Accounts lg = accDao.readByEmail(email);
            lg.setPassword(newPass);
            tx = session.beginTransaction();
            session.save(lg);
            tx.commit();
            return 1;
        } catch (Exception e) {
            tx.rollback();
            return 0;
        }
    }

    /**
     * @param ResetRequests some fields entered to create
     * @return int
     * @see Form to reset
     */
    public int create(ResetRequests rq) {
        try {

            tx = session.beginTransaction();
            session.save(rq);
            tx.commit();
            return 1;
        } catch (Exception e) {
            tx.rollback();
            return 0;
        }
    }
}
